package com.zhongxi.guoyuan.view.seller;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.beautifulStoreOrder.BillAdapter;
import com.zhongxi.guoyuan.adapter.seller.SellerRewardCoinAdapter;
import com.zhongxi.guoyuan.adapter.shopCart.AddressAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.LogisticsBaseResult;
import com.zhongxi.guoyuan.entity.RewardCoinSellerResult;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinOrderResult;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinSearchResult;
import com.zhongxi.guoyuan.entity.seller.BillData;
import com.zhongxi.guoyuan.entity.seller.BillResult;
import com.zhongxi.guoyuan.entity.shopcart.AddressList;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.MyTextUtils;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;
import com.zhongxi.guoyuan.widget.view.MyScrollView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class SellerRewardCoinActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener, MyScrollView.OnScrollListener {

    private TextView tvPhone;
    private TextView tvId;
    private TextView tvAllNum;
    private TextView tvCashNum;
    private TextView tvCoinNum;
    private TextView tvOrder;
    private TextView tvBill;
    private EditText etPhone;
    private TextView tvQuery;
    private NoScrollListView lvList;
    private TextView tvAllOrderNum;
    private TextView tvPostNum;
    private TextView tvGetNum;
    private String type;
    private TextView tvStore;
    private TextView tvStartTime;
    private TextView tvEndTime;

    private String startTime;
    private String endTime;
    private DatePickerDialog dialog;
    private Map<String, String> map = new HashMap<>();
    private int flag = 1;
    private int status = 0;
    private RadioButton rb0, rb1, rb2;
    private String url = Url.REWARD_SEND;;
    private List<RewardCoinOrderResult.RewardCoinOrder> data = new ArrayList<>();
    private SellerRewardCoinAdapter adapter;
    private int inflag = 0;
    private boolean first = true;

    private List<AddressList.AddressData> list = new ArrayList<AddressList.AddressData>();
    private List<AddressList.AddressData> cityList = new ArrayList<AddressList.AddressData>();
    private List<AddressList.AddressData> preList = new ArrayList<AddressList.AddressData>();

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    //叫快递
                    showLogistics((int)msg.obj);
                    break;
                case 1:
                    //确认收货
                    sureGet((int)msg.obj);
                    break;
                case 2:
                    sendLogistics((int)msg.obj);
                    break;
            }
        }
    };

    /**
     * 发快递
     * @param obj
     */
    private void sendLogistics(final int obj) {
        View view = ((Activity) context).getLayoutInflater().inflate(R.layout.send_logistics, null);
        final Dialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.getWindow().setContentView(view);

        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

        ImageView ivCancel = (ImageView) view.findViewById(R.id.ivCancel);
        Spinner sp = (Spinner) view.findViewById(R.id.sp);
        final EditText tvOrder = (EditText) view.findViewById(R.id.tvOrder);
        TextView tvName = (TextView) view.findViewById(R.id.tvName);
        final TextView tvPhone = (TextView) view.findViewById(R.id.tvPhone);
        final EditText tvAddress = (EditText) view.findViewById(R.id.tvAddress);
        TextView btSubmit = (TextView) view.findViewById(R.id.btSubmit);

        final List<AddressList.AddressData> logisticsList = setLogistics(sp);

        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                logistPos = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        if(null!=data.get(obj)&&!"".equals(data.get(obj))){
            if(null!=data.get(obj).getOrderInfo()&&!"".equals(data.get(obj).getOrderInfo())){
                RewardCoinOrderResult.OrderInfo orderInfo = data.get(obj).getOrderInfo();
                if(null!=orderInfo.getConsignee()&&!"".equals(orderInfo.getConsignee())){
                    tvName.setText("姓名："+orderInfo.getConsignee());
                }else{
                    tvName.setText("");
                }
                if(null!=orderInfo.getConsigneemphone()&&!"".equals(orderInfo.getConsigneemphone())){
                    tvPhone.setText("电话："+orderInfo.getConsigneemphone());
                }
                if(null!=orderInfo.getShippingaddress()&&!"".equals(orderInfo.getShippingaddress())){
                    tvAddress.setText(orderInfo.getShippingaddress());
                }
            }
        }

        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        /**
         * 发快递
         */
        btSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!MyTextUtils.isFastDoubleClick()){
                    String order= tvOrder.getText().toString().trim();
                    String address = tvAddress.getText().toString().trim();
                    if(TextUtils.isEmpty(order)){
                        showShortToast("请输入运单号");
                    }else if(TextUtils.isEmpty(address)){
                        showShortToast("请输入详细地址");
                    }else{
                        toSendLogistics(logisticsList,order, obj);
                    }
                }
                dialog.dismiss();
            }
        });

    }

    /**
     * 发快递
     * @param logisticsList
     * @param order
     * @param obj
     */
    private void toSendLogistics(List<AddressList.AddressData> logisticsList, String order, int obj) {
        type = "send";
        map.clear();
        map.put("orderid", data.get(obj).getOrderid());
        map.put("expressCode", logisticsList.get(logistPos).getKey());
        map.put("logisticscode", order);
        getServer(Url.MAIL_ORDER, map, "", true, null, "post");
    }

    private AddressAdapter provinceAdapter;
    private AddressAdapter cityAdapter;
    private AddressAdapter preAdapter;
    private AddressList addressResult;
    private AddressList cityResult;
    private AddressList addResult;
    private int addressPos;
    private boolean refresh;
    private int cityPos;
    private int prePos;
    private int logistPos;

    /**
     * 弹窗（预约快递）
     * @param obj
     */
    private void showLogistics(final int obj) {
        View view = ((Activity) context).getLayoutInflater().inflate(R.layout.item_send_logistics, null);
        final Dialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.getWindow().setContentView(view);

        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

        ImageView ivCancel = (ImageView) view.findViewById(R.id.ivCancel);
        final Spinner sp1 = (Spinner) view.findViewById(R.id.sp1);
        final Spinner sp2 = (Spinner) view.findViewById(R.id.sp2);
        final Spinner sp3 = (Spinner) view.findViewById(R.id.sp3);
        final Spinner sp4 = (Spinner) view.findViewById(R.id.sp4);
        final EditText etDetail = (EditText) view.findViewById(R.id.etDetail);
        final EditText tvName = (EditText) view.findViewById(R.id.tvName);
        final EditText tvPhone = (EditText) view.findViewById(R.id.tvPhone);

        TextView btSubmit = (TextView) view.findViewById(R.id.btSubmit);

        provinceAdapter = new AddressAdapter(this, list, 1);
        sp1.setAdapter(provinceAdapter);
        cityAdapter = new AddressAdapter(this, cityList, 2);
        sp2.setAdapter(cityAdapter);
        preAdapter = new AddressAdapter(this, preList, 3);
        sp3.setAdapter(preAdapter);
        getProvince();

        final List<AddressList.AddressData> logisticsList = setLogistics(sp4);
        sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sp1.setSelection(position);
                addressPos = position;
                if(refresh)
                    getCity();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sp2.setSelection(position);
                cityPos = position;
                if(refresh)
                    getPrefecture();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        sp3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                prePos = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        sp4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                logistPos = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //预约取件
        btSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!MyTextUtils.isFastDoubleClick()){
                    String detail = etDetail.getText().toString().trim();
                    String name = tvName.getText().toString().trim();
                    String phone = tvPhone.getText().toString().trim();
                    if(null==detail||"".equals(detail)){
                        showShortToast("请输入详细地址");
                    }else if(null==name||"".equals(name)){
                        showShortToast("请输入您的姓名");
                    }else if(null==phone||"".equals(phone)){
                        showShortToast("请输入您的手机号");
                    }else{
                        getLogistics(detail, name, phone,logisticsList, obj);
                    }
                }
                dialog.dismiss();
            }
        });

    }

    @NonNull
    private List<AddressList.AddressData> setLogistics(Spinner sp) {
        final List<AddressList.AddressData> logisticsList = new ArrayList<>();
        try {
            JSONObject express = new JSONObject(MyApplication.getInstance().getLogistics());
            Iterator<String> iterator = express.keys();
            while (iterator.hasNext()) {
                AddressList.AddressData result = new AddressList.AddressData();
                String key = iterator.next();
                String value = express.getString(key);
                result.setKey(key);
                result.setValue(value);
                logisticsList.add(result);
            }

            AddressAdapter logisticsAdapter = new AddressAdapter(this, logisticsList, 4);
            sp.setAdapter(logisticsAdapter);
        }catch (Exception e){
            e.printStackTrace();
        }
        return logisticsList;
    }

    /**
     * 获取省份列表
     */
    private void getProvince() {
        type = "province";
        getServer(Url.PROVINCE_LIST, null, "", false, null, "get");
    }

    /**
     * 获取城市列表
     */
    private void getCity() {
        type = "city";
        String str = "&pid="+addressResult.getData().get(addressPos).getP_id();
        getServer(Url.CITY_LIST, null, str, false, null, "get");
    }

    /**
     * 获取区县列表
     */
    private void getPrefecture() {
        type = "prefecture";
        String str = "&pid="+addressResult.getData().get(addressPos).getP_id()+"&cid="+cityResult.getData().get(cityPos).getC_id();
        Log.i("base", str);
        getServer(Url.PREFECTURE_LIST, null, str, false, null, "get");
    }

    /**
     * 预约取件
     * @param detail
     * @param name
     * @param phone
     * @param logisticsList
     * @param obj
     */
    private void getLogistics(String detail, String name, String phone, List<AddressList.AddressData> logisticsList, int obj) {
        type = "logistics";
        map.clear();
        map.put("orderid",data.get(obj).getOrderid());
        map.put("expressCode", logisticsList.get(logistPos).getKey());
        map.put("province", list.get(addressPos).getProvince());
        map.put("city", cityList.get(cityPos).getCity());
        map.put("prefecture", preList.get(prePos).getPrefecture());
        map.put("address", detail);
        map.put("mphone", phone);
        map.put("name", name);
        getServer(Url.RESERVATION, map, "", true, null, "post");
    }

    private Spinner sp;
    private String selectPos = "1";
    private TextView tvTips;
    private LinearLayout linearTitle;
    private LinearLayout linearBill;
    private MyScrollView myScrollView;
    private boolean scroll;

    /**
     * 确认提货
     * @param pos
     */
    private void sureGet(int pos) {
        type = "sure";
        map.clear();
        map.put("orderid", data.get(pos).getOrderid());
        getServer(Url.CONFIRM_ORDER, map, "", true, null, "post");
    }

    private BillAdapter billAdapter;
    private List<BillData> billData = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_seller_reward_coin);
        initTitle();
        tvTitle.setText("返银超市供货商");
        tvPhone = (TextView) findViewById(R.id.tvPhone);
        tvId = (TextView) findViewById(R.id.tvId);
        tvTips = (TextView) findViewById(R.id.tvTips);
        myScrollView = (MyScrollView) findViewById(R.id.myScrollView);

        tvStore = (TextView) findViewById(R.id.tvStore);
        tvAllNum = (TextView) findViewById(R.id.tvAllNum);
        tvCashNum = (TextView) findViewById(R.id.tvCashNum);
        tvCoinNum = (TextView) findViewById(R.id.tvCoinNum);
        tvAllOrderNum = (TextView) findViewById(R.id.tvAllOrderNum);
        tvPostNum = (TextView) findViewById(R.id.tvPostNum);
        tvGetNum = (TextView) findViewById(R.id.tvGetNum);
        tvOrder = (TextView) findViewById(R.id.tvOrder);
        tvBill = (TextView) findViewById(R.id.tvBill);

        linearBill = (LinearLayout) findViewById(R.id.linearBill);
        linearTitle = (LinearLayout) findViewById(R.id.linearTitle);

        tvStartTime = (TextView) findViewById(R.id.tvStartTime);
        tvEndTime = (TextView) findViewById(R.id.tvEndTime);

        etPhone = (EditText) findViewById(R.id.etPhone);
        tvQuery = (TextView) findViewById(R.id.tvQuery);
        lvList = (NoScrollListView) findViewById(R.id.lvList);
        sp = (Spinner) findViewById(R.id.sp);

        rb0 = (RadioButton) findViewById(R.id.rb0);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        tvOrder.setSelected(true);
    }

    @Override
    protected void initData() {
        super.initData();
        if(inflag == 0){
            adapter = new SellerRewardCoinAdapter(context, data, handler);
            lvList.setAdapter(adapter);
        }else if(inflag == 1){
            billAdapter = new BillAdapter(context, billData);
            lvList.setAdapter(billAdapter);
        }
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(now);
        tvStartTime.setText(dateNowStr);
        tvEndTime.setText(dateNowStr);
        startTime = endTime = dateNowStr;
        getMallInfo();
    }

    /**
     * 控制台信息
     */
    private void getMallInfo() {
        type = "info";
        getServer(Url.REWARD_INFO, null, "", true, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        myScrollView.setOnScrollListener(this);
        ivBack.setOnClickListener(this);
        tvStartTime.setOnClickListener(this);
        tvEndTime.setOnClickListener(this);
        tvQuery.setOnClickListener(this);
        rb0.setOnClickListener(this);
        rb1.setOnClickListener(this);
        rb2.setOnClickListener(this);
        tvOrder.setOnClickListener(this);
        tvBill.setOnClickListener(this);
        lvList.setOnItemClickListener(this);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectPos = position + 1+"";
                if(!first){
                    getBill();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvStartTime:
                showDateDialog(0);
                break;
            case R.id.tvEndTime:
                showDateDialog(1);
                break;
            case R.id.tvQuery:
                pageNum = 1;
                if(inflag == 0){
                    getData();
                }else if(inflag == 1){
                    getBill();
                }
                break;
            case R.id.rb0:
                status = 0;
                pageNum = 1;
                rb0.setChecked(true);
                rb1.setChecked(false);
                rb2.setChecked(false);
                data.clear();
                url = Url.REWARD_SEND;
                getData();
                break;
            case R.id.rb1:
                status = 1;
                pageNum = 1;
                rb0.setChecked(false);
                rb1.setChecked(true);
                rb2.setChecked(false);
                data.clear();
                url = Url.REWARD_COMPLAIN;
                getData();
                break;
            case R.id.rb2:
                status = 2;
                pageNum = 1;
                rb0.setChecked(false);
                rb1.setChecked(false);
                rb2.setChecked(true);
                data.clear();
                url = Url.REWARD_GET;
                getData();
                break;
            case R.id.tvOrder:
                inflag = 0;
                sp.setVisibility(View.GONE);
                linearTitle.setVisibility(View.VISIBLE);
                linearBill.setVisibility(View.GONE);
                tvTips.setText("下单手机号：");
                tvOrder.setSelected(true);
                tvBill.setSelected(false);
                data.clear();
                getMallInfo();
                break;
            case R.id.tvBill:
                inflag = 1;
                pageNum = 1;
                sp.setVisibility(View.VISIBLE);
                linearTitle.setVisibility(View.GONE);
                linearBill.setVisibility(View.VISIBLE);
                tvTips.setText("订单号：");
                tvOrder.setSelected(false);
                tvBill.setSelected(true);
                getBill();
                break;
        }
    }

    /**
     * 获取订单明细
     */
    private void getBill() {
        if(CommonTool.TimeCompare(startTime, endTime)){
            type = "bill";
            scroll = false;
            String str = "&page="+pageNum;
            map.clear();
            map.put("stime", startTime);
            map.put("etime", endTime);
            map.put("ordertype", selectPos);
            map.put("orderid", etPhone.getText().toString().trim());
            getServer(Url.REWARD_ORDER_DETAILS, map, str, true, null, "post");
        }else {
            showShortToast("结束时间不能小于开始时间");
        }
    }

    /**
     * 获取订单
     */
    private void getData() {
        type = "order";
        scroll = false;
        map.clear();
        map.put("userphone", etPhone.getText().toString().trim());
        String str = "";
        if(!url.equals(Url.REWARD_COMPLAIN)){
            str = "&page="+pageNum;
        }
        getServer(url, map, str, true, null, "post");
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private void showDateDialog(final int index) {
        Calendar c = Calendar.getInstance();
        dialog = new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String mon = month+1+"";
                String text = year + "-" + mon + "-" + dayOfMonth;
                if(index == 0){
                    tvStartTime.setText(text);
                    startTime = text;
                }else if(index == 1){
                    tvEndTime.setText(text);
                    endTime = text;
                }
                if(inflag == 0){
                    getRecorder();
                }else if(inflag == 1){
                    pageNum=1;
                    getBill();
                }
            }
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        dialog.setTitle("请选择时间：");
        dialog.getDatePicker().setCalendarViewShown(false);
        dialog.show();
    }

    /**
     * 商家中心-时间段内信息汇总
     */
    private void getRecorder() {
        if(CommonTool.TimeCompare(startTime, endTime)) {
            type = "data";
            map.clear();
            map.put("stime", startTime);
            map.put("etime", endTime);
            map.put("type", flag+"");
            getServer(Url.REWARD_DATA, map, "", true, null, "post");
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("info".equals(type)){
                RewardCoinSellerResult result = gson.fromJson(response, RewardCoinSellerResult.class);
                if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                initInfo(result);
            }else if("data".equals(type)){
                RewardCoinSearchResult result = gson.fromJson(response, RewardCoinSearchResult.class);
                if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                if(null!=result.getOrderNum()&&!"".equals(result.getOrderNum())){
                    tvAllOrderNum.setText(result.getOrderNum()+"单");
                }else{
                    tvAllOrderNum.setText("*单");
                }
                if(null!=result.getPostOrderNum()&&!"".equals(result.getPostOrderNum())){
                    tvPostNum.setText(result.getPostOrderNum()+"单");
                }else{
                    tvPostNum.setText("*单");
                }
                if(null!=result.getTodoorOrderNum()&&!"".equals(result.getTodoorOrderNum())){
                    tvGetNum.setText(result.getTodoorOrderNum()+"单");
                }else{
                    tvGetNum.setText("*单");
                }
                getData();
            }else if("order".equals(type)){
                RewardCoinOrderResult result = gson.fromJson(response, RewardCoinOrderResult.class);
                if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                adapter.setStatus(status);
                JSONObject obj = new JSONObject(response);
                JSONArray info = obj.getJSONArray("data");
                result.setInfo(info);
                if(status == 1){
                    JSONObject complaints = null;
                    JSONObject userinfo = null;
                    try {
                        complaints = obj.getJSONObject("complaints");
                        userinfo = obj.getJSONObject("userInfo");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    adapter.setInfo(complaints, userinfo);
                }else if(status == 2) {
                    JSONObject userinfo = null;
                    try {
                        userinfo = obj.getJSONObject("userInfo");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    adapter.setInfo( userinfo);
                }
                if (pageNum == 1) {
                    data.clear();
                }
                if (result.getData().size() > 0) {
                    data.addAll(result.getData());
                }
                adapter.notifyDataSetChanged();
                scroll = true;
            }else if("sure".equals(type)){
                BaseResponse result = gson.fromJson(response, BaseResponse.class);
                if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                showShortToast("确认成功");
                getData();
            }else if("bill".equals(type)){
                first = false;
                scroll = true;
                BillResult result = gson.fromJson(response, BillResult.class);
                if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                if(pageNum==1){
                    billData.clear();
                }
                billData.addAll(result.getData());
                billAdapter = new BillAdapter(context, billData);
                lvList.setAdapter(billAdapter);
            }else if("logistics".equals(type)){
                BaseResponse result = gson.fromJson(response, BaseResponse.class);
                if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                showShortToast("快递预约成功！");
                adapter.notifyDataSetChanged();
            }else if("province".equals(type)){
                addressResult = gson.fromJson(response, AddressList.class);
                if(null!=addressResult.getSafeobj()&&!"".equals(addressResult.getSafeobj())){
                    MyApplication.getInstance().setSpValue(addressResult.getSafeobj());
                }
                list.clear();
                list.addAll(addressResult.getData());
                provinceAdapter.notifyDataSetChanged();
                getCity();
            }else if("city".equals(type)){
                cityResult = gson.fromJson(response, AddressList.class);
                if(null!=cityResult.getSafeobj()&&!"".equals(cityResult.getSafeobj())){
                    MyApplication.getInstance().setSpValue(cityResult.getSafeobj());
                }
                cityList.clear();
                cityList.addAll(cityResult.getData());
                cityAdapter.notifyDataSetChanged();
                getPrefecture();
            }else if("prefecture".equals(type)){
                addResult = gson.fromJson(response, AddressList.class);
                if(null!=addResult.getSafeobj()&&!"".equals(addResult.getSafeobj())){
                    MyApplication.getInstance().setSpValue(addResult.getSafeobj());
                }
                preList.clear();
                preList.addAll(addResult.getData());
                preAdapter.notifyDataSetChanged();
                refresh = true;
            }else if("send".equals(type)){
                BaseResponse result = gson.fromJson(response, BaseResponse.class);
                if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                showShortToast("发货成功！");
                getData();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 初始化控制台数据
     * @param result
     */
    private void initInfo(RewardCoinSellerResult result) {
        try{
            if (null != result.getOrderNum() && !"".equals(result.getOrderNum())) {
                tvAllNum.setText(result.getOrderNum());
            }else{
                tvAllNum.setText("");
            }
            if (null != result.getTotalFee() && !"".equals(result.getTotalFee())) {
                tvCashNum.setText(result.getTotalFee());
            }else{
                tvCashNum.setText("");
            }
            if (null != result.getTotalSilver() && !"".equals(result.getTotalSilver())) {
                tvCoinNum.setText(result.getTotalSilver());
            }else{
                tvCoinNum.setText("");
            }
            if(null!= result.getStoreInfo()&&!"".equals(result.getStoreInfo())){
                RewardCoinSellerResult.RewardCoinSellerData storeInfo = result.getStoreInfo();
                if(null!=storeInfo.getName()&&!"".equals(storeInfo.getName())){
                    tvStore.setText(storeInfo.getName());
                }else{
                    tvStore.setText("");
                }
                if(null!=storeInfo.getMphone()&&!"".equals(storeInfo.getMphone())){
                    tvPhone.setText("电话:"+storeInfo.getMphone());
                }else{
                    tvPhone.setText("");
                }
                if(null!=storeInfo.getId()&&!"".equals(storeInfo.getId())){
                    tvId.setText("ID:"+storeInfo.getId());
                }else{
                    tvId.setText("");
                }
            }
            getRecorder();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if(9002==result.getCode()){
                showShortToast("登陆已失效，请重新登陆");
                SharedPreferences sharedPrefrences = getSharedPreferences(Url.APP_SP, MODE_PRIVATE);

                sharedPrefrences.edit().putString("coincredentials", "").commit();
                startAct(SellerRewardLoginActivity.class);
            }else if(9102 == result.getCode()){
                LogisticsBaseResult logistics = gson.fromJson(response, LogisticsBaseResult.class);
                showShortToast(logistics.getData().getErrorMsg());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == adapter.getmExpandedMenuPos()) {
            adapter.setmExpandedMenuPos(-1);
        } else {
            adapter.setmExpandedMenuPos(position);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onScroll(int scrollY) {

    }

    @Override
    public void scrollBottom() {
        if(!MyTextUtils.isFastDoubleClick()&&scroll&&adapter.getmExpandedMenuPos()==-1){
            pageNum ++;
            if(inflag == 1){
                getBill();
            }else if(inflag == 0){
                getData();
            }
        }
    }
}
